<template>
  <div class="divBox">
    <el-card>
      <div class="mb20">
        <el-button size="small" type="primary" @click="onAdd">添加徽章</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="small" highlight-current-row>
        <el-table-column label="ID" prop="user_merit_id" min-width="60" />
        <el-table-column label="名称" prop="merit_name" min-width="100" />
        <el-table-column label="等级图标" min-width="100">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image style="width: 36px; height: 36px"
                :src="scope.row.merit_icon ? scope.row.merit_icon : ''"
                :preview-src-list="[scope.row.merit_icon || '']" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="人数" prop="user_num" min-width="100" />
        <el-table-column label="所需功德值" prop="merit_value" min-width="120" />
        <el-table-column prop="create_time" label="创建时间" min-width="150" />
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row.user_merit_id)">编辑</el-button>
            <el-button type="text" size="small"
              @click="handleDelete(scope.row.user_merit_id, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination background :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
  // +----------------------------------------------------------------------
  // | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
  // +----------------------------------------------------------------------
  // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  // +----------------------------------------------------------------------
  // | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
  // +----------------------------------------------------------------------
  // | Author: CRMEB Team <admin@crmeb.com>
  // +----------------------------------------------------------------------
  import {
    meritLstApi,
    addMeritApi,
    meritUpdateApi,
    meritDeleteApi,
  } from "@/api/user";
  export default {
    name: "Classify",
    data() {
      return {
        tableData: {
          data: [],
          total: 0
        },
        tableFrom: {
          page: 1,
          limit: 20,
          keyword: ""
        },
        listLoading: true
      };
    },
    mounted() {
      this.getList();
    },
    methods: {
      // 列表
      getList() {
        this.listLoading = true;
        meritLstApi(this.tableFrom)
          .then(res => {
            this.tableData.data = res.data.list;
            this.tableData.total = res.data.count;
            this.listLoading = false;
          })
          .catch(res => {
            this.listLoading = false;
            this.$message.error(res.message);
          });
      },
      pageChange(page) {
        this.tableFrom.page = page;
        this.getList();
      },
      handleSizeChange(val) {
        this.tableFrom.limit = val;
        this.getList();
      },
      // 添加
      onAdd() {
        this.$modalForm(addMeritApi()).then(() => this.getList())
      },
      // 编辑
      onEdit(id) {
        this.$modalForm(meritUpdateApi(id)).then(() => this.getList())
      },
      // 删除
      handleDelete(id, idx) {
        this.$modalSure('删除该会员').then(() => {
          meritDeleteApi(id).then(({
            message
          }) => {
            this.$message.success(message)
            this.tableData.data.splice(idx, 1)
          }).catch(({
            message
          }) => {
            this.$message.error(message)
          })
        })
      }
    }
  };
</script>

<style scoped lang="scss"></style>
